{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.导入模块\n",
    "import requests\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.输入face++官网给的api_secret和api_key\n",
    "api_secret = \"vGEgDqiSEbiPlcEx0ggY8TP9Ch_7n0lh\"\n",
    "api_key = '_fV9pDujMV301iO8z0PP7tMpPulyCaMl'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3.detect face 添加一张人脸的图片，检测人脸\n",
    "api_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "img_url = \"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603534564342&di=64a2b527aed7e83ff904f01950b22940&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F201911%2F17%2F20191117195100_vtwef.thumb.400_0.jpg\"\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "r = requests.post(api_url, params=payload, headers=headers)\n",
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603524562,9c3d0b36-e7f2-419f-a547-1599ff3a9bde',\n",
       " 'time_used': 278,\n",
       " 'faces': [{'face_token': '1d5d6cc2613425ddf461fc23971f5397',\n",
       "   'face_rectangle': {'top': 70, 'left': 137, 'width': 178, 'height': 178},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 23},\n",
       "    'smile': {'value': 0.0, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.013,\n",
       "     'disgust': 0.016,\n",
       "     'fear': 0.013,\n",
       "     'happiness': 0.117,\n",
       "     'neutral': 81.228,\n",
       "     'sadness': 12.424,\n",
       "     'surprise': 6.188}}}],\n",
       " 'image_id': '5ECQAfB52+R8LP4JjiSNNw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'e928099b1defddbf8f0f4bc7fb1bd3e8',\n",
       " 'time_used': 184,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1603524720,e6fdd753-9972-4ea0-a0ec-36fd8e457e24',\n",
       " 'outer_id': 'zhaolusi',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4.compare face 创建一个人脸的集合，存储人脸标识face_token一个FaceSet能够存储10000个face_token。\n",
    "api_secret = \"vGEgDqiSEbiPlcEx0ggY8TP9Ch_7n0lh\"\n",
    "api_key = '_fV9pDujMV301iO8z0PP7tMpPulyCaMl'\n",
    "display_name = \"相册\" # 人脸集合的名字\n",
    "outer_id = \"zhaolusi\" # 账号下全局唯一的FaceSet自定义标识，可以用来管理 FaceSet 对象。\n",
    "api_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    # 请求参数\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'display_name':display_name,\n",
    "    'outer_id':outer_id,\n",
    "}\n",
    "r = requests.post(api_url, params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'e928099b1defddbf8f0f4bc7fb1bd3e8',\n",
       " 'tags': '',\n",
       " 'time_used': 95,\n",
       " 'user_data': '',\n",
       " 'display_name': '相册',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603524748,e936253a-b884-4c85-922e-39fbb517bd26',\n",
       " 'outer_id': 'zhaolusi'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 5.FaceSet GetDetail 获取一个FaceSet的所有信息，包括此FaceSet的faceset_token,outer_id,display_name的信息，以及此FaceSet中存放的face_token数量与列表。\n",
    "api_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'outer_id':outer_id,\n",
    "}\n",
    "r = requests.post(api_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'e928099b1defddbf8f0f4bc7fb1bd3e8',\n",
       " 'time_used': 674,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1603524919,dba413a0-6d56-43ed-9f31-3e37d9beac8c',\n",
       " 'outer_id': 'zhaolusi',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "6. # FaceSet AddFace 为已经创建好的FaceSet添加人脸标识face_token.\n",
    "api_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'e928099b1defddbf8f0f4bc7fb1bd3e8',\n",
    "    'face_tokens':'1d5d6cc2613425ddf461fc23971f5397',\n",
    "}\n",
    "r = requests.post(api_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'e928099b1defddbf8f0f4bc7fb1bd3e8',\n",
       " 'face_removed': 1,\n",
       " 'time_used': 201,\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603525024,628ce3e6-c789-4ebe-bd3b-b40933d74df0',\n",
       " 'outer_id': 'zhaolusi',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "7. # FaceSet RemoveFace 移除一个FaceSet中的某些或者全部face_token\n",
    "api_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/removeface\"\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'e928099b1defddbf8f0f4bc7fb1bd3e8',\n",
    "    'face_tokens':'1d5d6cc2613425ddf461fc23971f5397',\n",
    "}\n",
    "r = requests.post(api_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'e928099b1defddbf8f0f4bc7fb1bd3e8',\n",
       " 'request_id': '1603525060,139c0bb5-e6b6-47d6-a380-1e7e5f50cdf2',\n",
       " 'time_used': 72,\n",
       " 'outer_id': 'zhaolusi'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 8.FaceSet Update 更新人脸集合的属性\n",
    "api_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'e928099b1defddbf8f0f4bc7fb1bd3e8',\n",
    "    'display_name':\"相册\",\n",
    "}\n",
    "r = requests.post(api_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 9.1检测两张需要对比的人脸，获得face_token\n",
    "api_secret = \"vGEgDqiSEbiPlcEx0ggY8TP9Ch_7n0lh\"\n",
    "api_key = '_fV9pDujMV301iO8z0PP7tMpPulyCaMl'\n",
    "api_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "zrn = \"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603535266559&di=6f8b88ca1a5cedb6eced6ac4385d8a7a&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202009%2F18%2F20200918231607_8d90a.thumb.1000_0.jpg\"\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "payload = {\n",
    "    \"image_url\":zrn,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "r = requests.post(api_url, params=payload, headers=headers)\n",
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603525219,de0b42f9-6cb2-4410-b4fc-3ae96013919b',\n",
       " 'time_used': 391,\n",
       " 'faces': [{'face_token': 'c18006563b4aea01921f741820576f37',\n",
       "   'face_rectangle': {'top': 195, 'left': 329, 'width': 334, 'height': 334},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 32},\n",
       "    'smile': {'value': 0.092, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.006,\n",
       "     'disgust': 0.022,\n",
       "     'fear': 0.006,\n",
       "     'happiness': 0.447,\n",
       "     'neutral': 96.478,\n",
       "     'sadness': 0.042,\n",
       "     'surprise': 2.999}}}],\n",
       " 'image_id': 'ylMBdvtY/tw4R/STQQXCUw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 9.2 第二张人脸图片\n",
    "api_secret = \"vGEgDqiSEbiPlcEx0ggY8TP9Ch_7n0lh\"\n",
    "api_key = '_fV9pDujMV301iO8z0PP7tMpPulyCaMl'\n",
    "api_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "zls = \"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603535396449&di=57c3051fe091fe0e8dd82f191efbc9f7&imgtype=0&src=http%3A%2F%2Fimgsa.baidu.com%2Fbaike%2Fpic%2Fitem%2F267f9e2f07082838da47ac79b799a9014c08f111.jpg\"\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "payload = {\n",
    "    \"image_url\":zls,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "r = requests.post(api_url, params=payload, headers=headers)\n",
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603525363,a67648bb-fdc9-4dcc-8b7e-8abba4c5c5ba',\n",
       " 'time_used': 673,\n",
       " 'faces': [{'face_token': '5118fbc4f0cf6cd435fa30da8a7a9f1c',\n",
       "   'face_rectangle': {'top': 178, 'left': 503, 'width': 464, 'height': 464},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 22},\n",
       "    'smile': {'value': 96.212, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.008,\n",
       "     'disgust': 0.008,\n",
       "     'fear': 0.018,\n",
       "     'happiness': 95.569,\n",
       "     'neutral': 4.263,\n",
       "     'sadness': 0.123,\n",
       "     'surprise': 0.011}}}],\n",
       " 'image_id': 'v0yGX9B9xjPnyVRNLDe/QQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'confidence': 38.645,\n",
       " 'request_id': '1603525513,07aa8924-0bd5-4880-a9a7-3ad3c6ab142f',\n",
       " 'time_used': 435,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101}}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 10.Compare Face 将两个人脸进行比对，来判断是否为同一个人，返回比对结果置信度和不同误识率下的阈值.\n",
    "api_secret = \"vGEgDqiSEbiPlcEx0ggY8TP9Ch_7n0lh\"\n",
    "api_key = '_fV9pDujMV301iO8z0PP7tMpPulyCaMl'\n",
    "api_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "zls = '5118fbc4f0cf6cd435fa30da8a7a9f1c'\n",
    "zrn = 'c18006563b4aea01921f741820576f37'\n",
    "             # 第二张人脸图片的url\n",
    "payload ={\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'face_token1': zls,\n",
    "    'face_token2': zrn\n",
    "}\n",
    "r = requests.post(api_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6rc1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
